/*
 * Project: jcerticles
 * Copyright(c) 2011 jcerticles
 * All rights reserved.
 *
 * $Revision $, last modified $Date $ by $Author $
 */
package com.quintilis31.home.core.content.repository.impl;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Query;

import com.quintilis31.home.common.repository.content.Answer;
import com.quintilis31.home.common.repository.content.Content;
import com.quintilis31.home.common.repository.content.Question;
import com.quintilis31.home.core.content.repository.IContentDAO;
import com.quintilis31.home.persistence.type.impl.sql.jpa.jpaimpl.GenericJPADAO;
import com.quintilis31.home.persistence.util.ContentSQLStatements;

/**
 * TODO... Write javadoc...
 * @author Jothi
 * @since  1.6
 */
public class ContentDAOImpl<T, ID extends Serializable> extends GenericJPADAO < T, ID > implements IContentDAO {
	
	/* Constants to represent the column names in the respective tables */
	private static final String QUESTION_TECHNOLOGY_ID = "technology_id";	
	private static final String QUESTION_SUB_TECHNOLOGY_ID = "sub_technology_id";	
	private static final String QUESTION_ID = "question_id";
		
	/**
	 * 
	 * @param class1
	 */
	public ContentDAOImpl(Class<? extends Content> class1) {
		this.entityClass = (Class<T>) class1;
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public Question getRandomQuestion(final long technologyId) {
		return (Question)super.findById(technologyId);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public List < Content > getAllAnswersForQuestion(final long questionId) {
		return null;				
	}
	
	/**
	 * {@inheritDoc}
	 */
	public List < Question > getAllQuestionsForExam() {
		System.out.println(entityClass);
		return null;		
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List < Question > getAllQuestionsForExam(final long technologyId, final long subTechnologyId) {
		final Query allQuestionsForTechnology = entityManager.createNamedQuery(ContentSQLStatements.GET_ALL_QUESTIONS_FOR_TECHNOLOGY_ID);
		
		allQuestionsForTechnology.setParameter(QUESTION_TECHNOLOGY_ID, technologyId);
		allQuestionsForTechnology.setParameter(QUESTION_SUB_TECHNOLOGY_ID, subTechnologyId);		
		
		return (List < Question >) allQuestionsForTechnology.getResultList();
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List < Answer > getAllAnswersForQuestionId(final long questionId) {
		final Query allAnswersForQuestionId = entityManager.createNamedQuery(ContentSQLStatements.GET_ALL_ANSWERS_FOR_QUESTION_ID);
		
		allAnswersForQuestionId.setParameter(QUESTION_ID, questionId);
		
		return (List < Answer >) allAnswersForQuestionId.getResultList();
	}
}